package org.gd.exercise.learnspringbootredis.controller;

import org.gd.exercise.learnspringbootredis.dto.WxArticleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author name: yushanjiang
 * @version 1.0
 * @description: redis的List类型应用场景
 * @date 2025/9/12 11:25
 */
@RestController
@RequestMapping("/list")
public class ListController {
    @Autowired
    RedisTemplate redisTemplate;

    /**
    * @description: 场景1-队列、消息队列-插入消息
    * @param: [queueId, msg]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 16:39
    */
    @PostMapping("/msgqueue/push")
    public String pushQueue(@RequestParam String queueId, @RequestBody String msg) {
        //从右侧插入消息
        String key = "jys_msgqueue:" + queueId;
        redisTemplate.opsForList().rightPush(key, msg);
        return "success!msg:" + msg;
    }
    
    /**
    * @description: 场景1-队列、消息队列-取出消息
    * @param: [queueId]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 16:44
    */
    @PostMapping("/msgqueue/pop")
    public String popQueue(@RequestParam String queueId) {
        //从左侧取出消息
        String key = "jys_msgqueue:" + queueId;
        Object msg = redisTemplate.opsForList().leftPop(key);
        return "success!queueId:" + queueId + "\nmsg:" + msg;
    }

    /**
    * @description: 场景2-栈-入栈
    * @param: [stackId, item]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 17:05
    */
    @PostMapping("/stack/push")
    public String pushStack(@RequestParam String stackId, @RequestBody String item) {
        String key = "jys_stack:" + stackId;
        redisTemplate.opsForList().rightPush(key, item);
        return "success!stackId:" + stackId;
    }

    /**
    * @description: 场景2-栈-出栈
    * @param: [stackId]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 17:05
    */
    @PostMapping("/stack/pop")
    public String popStack(@RequestParam String stackId) {
        String key = "jys_stack:" + stackId;
        Object item = redisTemplate.opsForList().rightPop(key);
        return "success!stackId:" + stackId + "\nmsg:" + item;
    }

    /**
    * @description: 场景3-阻塞队列-插入元素
    * @param: [queueId, item]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 17:41
    */
    @PostMapping("/blockedqueue/push")
    public String pushBlockedQueue(@RequestParam String queueId, @RequestBody String item) {
        String key = "jys_blockedqueue:" + queueId;
        Long afterPushSize = redisTemplate.opsForList().leftPush(key, item);
        if (afterPushSize != null) {
            return "success!queueId:" + queueId + "\nafterPushSize:" + afterPushSize;
        } else {
            return "fail!queueId:" + queueId + "\nafterPushSize:" + afterPushSize;
        }
    }
    
    /**
    * @description: 场景3-阻塞队列-阻塞取出元素
    * @param: [queueId]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/12 17:41
    */
    @PostMapping("/blockedqueue/pop")
    public String popBlockedQueue(@RequestParam String queueId) {
        String key = "jys_blockedqueue:" + queueId;
        Object item = redisTemplate.opsForList().leftPop(key, 500, TimeUnit.SECONDS);
        if(item != null) {
            return "success!blockedqueueId:" + queueId + "\nqueue_size:" + item;
        } else {
            return "failed!超时，队列中没有插入元素";
        }
    }
}
